<template>
  <view class="filter-item">
    <text class="font-item-text">住房情况</text>
    <view class="uni-input" @click="togglePopup">
      <template v-if="selectedHousingStatus.length > 0">
        <text class="range-text">{{ selectedHousingStatus.join(', ') }}</text>
      </template>
      <template v-else>请选择住房情况</template>
      <uni-icons :type="iconType" size="20" class="icon"></uni-icons>
    </view>
  </view>

  <view v-show="showPopupContent" class="popup-content">
    <view class="button-container">
      <button
        v-for="status in housingStatusOptions"
        :key="status"
        :class="
          selectedHousingStatus.includes(status) ||
          (status == '不限' && selectedHousingStatus.length == 0)
            ? 'custom-button selected'
            : 'checkbox-label'
        "
        @click="() => changeHousingStatus(status)"
      >
        {{ status }}
        <image
          src="/static/search/true.png"
          v-if="
            selectedHousingStatus.includes(status) ||
            (status == '不限' && selectedHousingStatus.length == 0)
          "
          class="checkmark"
        ></image>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, defineEmits, defineProps, watch } from 'vue'

const props = defineProps<{
  housingsituation: string[]
}>()

const emit = defineEmits<{
  (e: 'update:housingsituation', value: string[]): void
}>()

const housingStatusOptions = [
  '不限',
  '己购房(无贷款)',
  '已购房(有贷款)',
  '与父母同住',
  '无房',
  '与人合租',
  '有能力购房',
  '无房希望双方解决',
  '独自租房',
  '住亲朋家',
  '住单位房',
]
const selectedHousingStatus = ref<string[]>([...props.housingsituation]) // 默认选择不限
const showPopupContent = ref(false)
const iconType = ref('arrowright')

watch(
  () => props.housingsituation,
  (newVal) => {
    selectedHousingStatus.value = [...newVal]
  },
)

const togglePopup = () => {
  showPopupContent.value = !showPopupContent.value
  iconType.value = showPopupContent.value ? 'up' : 'arrowright'
}

const changeHousingStatus = (status: string) => {
  if (status == '不限') {
    selectedHousingStatus.value = []
  } else {
    let idx = selectedHousingStatus.value.indexOf(status)
    if (idx == -1) {
      selectedHousingStatus.value.push(status)
    } else {
      selectedHousingStatus.value.splice(idx, 1)
    }
  }
  emit('update:housingsituation', selectedHousingStatus.value)
}
</script>

<style scoped>
.filter-item {
  font-size: 26rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  cursor: pointer;
  margin-bottom: 40rpx;
}
.font-item-text {
  color: #a9a5a3;
}

.uni-input {
  display: flex;
  align-items: center;
}

.range-text {
  color: #1e90ff;
}

.icon {
  color: #ccc;
  margin-left: 20rpx;
}

.popup-content {
  padding: 40rpx;
  background-color: #fff;
  width: 100%;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.15);
  z-index: 1000;
  margin-top: 20rpx;
}

.button-container {
  display: flex;
  flex-wrap: wrap;
  gap: 8rpx; /* 按钮之间的间距 */
}

.checkbox-label,
.custom-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: calc(50% - 8rpx); /* 每行两个按钮，并考虑间距 */
  padding: 0rpx;
  height: 80rpx;
  border-radius: 8rpx;
  box-sizing: border-box;
  font-size: 24rpx;
  position: relative;
  cursor: pointer;
  user-select: none;
  transition: all 0.3s;
  margin: 4rpx 0; /* 上下间距为4rpx，总间距为8rpx */
  line-height: 24rpx;
}

.checkbox-label {
  /* border: 2rpx solid #ff0000; */
  background-color: #f5f5f5;
  color: #666;
}

.custom-button {
  border: 2rpx solid #00a0e9;
  color: #00a0e9; /* 文字颜色 */
  background-color: #e5f7f9; /* 背景颜色 */
}

.custom-button.selected::before {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 35rpx 35rpx 22rpx 20rpx;
  border-color: transparent rgb(1, 170, 190) rgb(1, 170, 190) transparent;
}

.custom-button.selected .checkmark::before {
  content: '';
  color: white;
  font-size: 32rpx;
  position: absolute;
  top: 50%;
  left: 63%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.custom-button .checkmark {
  position: absolute;
  bottom: 4rpx;
  right: 4rpx;
  width: 28rpx;
  height: 28rpx;
  border-radius: 4rpx;
  text-align: center;
  /* line-height: 28rpx; */
}
</style>
